package com.jack.algorithm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class KthLargest {

    List<Integer> nums;
    int size;
    int k;

    public KthLargest(int k, int[] nums) {
        this.k = k;
        this.size = nums.length;
        this.nums = Arrays.stream(nums).boxed()
                .sorted(Comparator.comparing(Integer::intValue).reversed()).collect(Collectors.toList());
    }

    public int add(int val) {
        for(int i = 0;i < size;i++){
            if(i == 0 && val > nums.get(i)){
                nums.add(0, val);
                break;
            }
            if(i == size - 1 && val < nums.get(i)){
                nums.add(size, val);
                break;
            }
            if(val >= nums.get(i + 1) && val <= nums.get(i)){
                nums.add(i + 1, val);
                break;
            }
        }
        if(size == 0){
            nums.add(val);
        }
        size++;
        return nums.get(k - 1);
    }

    public static void main(String[] args) {
//        KthLargest kthLargest = new KthLargest(3, new int[]{4, 5, 8, 2});
//        System.out.println(kthLargest.add(3));
//        System.out.println(kthLargest.add(5));
//        System.out.println(kthLargest.add(10));
//        System.out.println(kthLargest.add(9));
//        System.out.println(kthLargest.add(4));
//
//        KthLargest kthLargest1 = new KthLargest(1, new int[]{});
//        System.out.println(kthLargest1.add(-3));
//        System.out.println(kthLargest1.add(-2));
//        System.out.println(kthLargest1.add(-4));
//        System.out.println(kthLargest1.add(0));
//        System.out.println(kthLargest1.add(4));
//
//        KthLargest kthLargest2 = new KthLargest(1, new int[]{-2});
//        System.out.println(kthLargest2.add(-3));
//        System.out.println(kthLargest2.add(0));
//        System.out.println(kthLargest2.add(2));
//        System.out.println(kthLargest2.add(-1));
//        System.out.println(kthLargest2.add(4));

//        System.out.println(new KthLargest(4,new int[]{}).getRow(33));

//        new KthLargest(2, new int[]{}).findDisappearedNumbers(new int[]{4,3,2,7,8,2,3,1});

        System.out.println(new KthLargest(2, new int[]{}).minSwapsCouples(new int[]{0,2,1,3}));
        System.out.println(new KthLargest(2, new int[]{}).minSwapsCouples(new int[]{3,2,0,1}));
        System.out.println(new KthLargest(2, new int[]{}).minSwapsCouples(new int[]{5,4,2,6,3,1,0,7}));
    }

    public int minSwapsCouples(int[] row) {
        int n = row.length;
        int result = 0;
        for(int i = 0;i < n - 1;i += 2){
            if(row[i] % 2 == 0){
                if(row[i + 1] - row[i] == 1){
                    continue;
                }
                for(int j = i + 1;j < n;j++){
                    if(row[j] == row[i] + 1){
                        int tmp = row[j];
                        row[j] = row[i + 1];
                        row[i + 1] = tmp;
                        result++;
                        break;
                    }
                }
            }else{
                if(row[i] - row[i + 1] == 1){
                    continue;
                }
                for(int j = i + 1;j < n;j++){
                    if(row[j] == row[i] - 1){
                        int tmp = row[j];
                        row[j] = row[i + 1];
                        row[i + 1] = tmp;
                        result++;
                        break;
                    }
                }
            }

        }
        return result;
    }

    static List<List<Integer>> rows = new ArrayList<>();
    static {
        rows.add(Arrays.asList(1));
        rows.add(Arrays.asList(1,1));
        for(int i = 2;i < 34;i++){
            List<Integer> tmp = new ArrayList<>();
            tmp.add(1);
            for(int j = 0;j < rows.get(i - 1).size() - 1;j++){
                tmp.add(rows.get(i - 1).get(j) + rows.get(i - 1).get(j + 1));
            }
            tmp.add(1);
            rows.add(tmp);
        }
    }
    public List<Integer> getRow(int rowIndex) {
        return rows.get(rowIndex);
    }

    public List<Integer> findDisappearedNumbers(int[] nums) {
        List<Integer> result = new ArrayList<>();
        int index = 0;
        while(index < nums.length){
            if(nums[index] != index + 1){
                int tmp = nums[index];
                while(nums[tmp - 1] != tmp){
                    int curTmp = nums[tmp - 1];
                    nums[tmp - 1] = tmp;
                    tmp = curTmp;
                }
            }
            index++;
        }
        for(int i = 0;i < nums.length;i++){
            if(nums[i] != i + 1){
                result.add(i + 1);
            }
        }
        return result;
    }
}
